#include "SoftSPI.hpp"

SoftSPI::SoftSPI(Pin *mosi,Pin *miso,Pin *sck){
    this->mosi = mosi;
    this->miso = miso;
    this->sck = sck;
}

int SoftSPI::write(unsigned char *buff,int size){
    this->delay();
    unsigned char byte;
    for ( int nbyte = 0; nbyte < size; nbyte++ ){
        byte = buff[nbyte];
        for ( int nbit = 0; nbit < 8; nbit++ ){
            if ( byte & 0x80 ){
                this->mosi->write(1);
            }else{
                this->mosi->write(0);
            }
            byte <<= 1;
            this->delay();
            this->sck->write(1);
            this->delay();
            this->sck->write(0);
            this->delay();
        }
    }
    return size;
}

int SoftSPI::read(unsigned char *buff,int size){
    /* 暂未实现 */
    return size;
}

int SoftSPI::transmit(unsigned char *txbuffer,unsigned char *rxbuffer,int size){
    /* 暂未实现 */
    return size;
}

void SoftSPI::delay(){
    for ( int n = 0; n < 50; n++ ){
        __asm__("nop");
    }
}